package com.lxmes.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.lxmes.business.domain.NcNoticeBill;
import com.lxmes.business.mapper.NcNoticeBillMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.NcGenAccountMapper;
import com.lxmes.business.domain.NcGenAccount;
import com.lxmes.business.service.INcGenAccountService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 应收/付账款Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-25
 */
@Service
public class NcGenAccountServiceImpl implements INcGenAccountService
{
    @Autowired
    private NcGenAccountMapper ncGenAccountMapper;
    @Autowired
    private NcNoticeBillMapper billMapper;

    /**
     * 查询应收/付账款
     *
     * @param id 应收/付账款主键
     * @return 应收/付账款
     */
    @Override
    public NcGenAccount selectNcGenAccountById(Long id)
    {
        return ncGenAccountMapper.selectNcGenAccountById(id);
    }

    /**
     * 查询应收/付账款列表
     *
     * @param ncGenAccount 应收/付账款
     * @return 应收/付账款
     */
    @Override
    public List<NcGenAccount> selectNcGenAccountList(NcGenAccount ncGenAccount)
    {
        ncGenAccount.setFactoryCode(SecurityUtils.getFactoryCode());
        return ncGenAccountMapper.selectNcGenAccountList(ncGenAccount);
    }

    /**
     * 新增应收/付账款
     *
     * @param ncGenAccount 应收/付账款
     * @return 结果
     */
    @Override
    public int insertNcGenAccount(NcGenAccount ncGenAccount)
    {
        ncGenAccount.setFactoryName(SecurityUtils.getFactoryName());
        ncGenAccount.setFactoryCode(SecurityUtils.getFactoryCode());
        return ncGenAccountMapper.insertNcGenAccount(ncGenAccount);
    }

    /**
     * 修改应收/付账款
     *
     * @param ncGenAccount 应收/付账款
     * @return 结果
     */
    @Override
    public int updateNcGenAccount(NcGenAccount ncGenAccount)
    {
        return ncGenAccountMapper.updateNcGenAccount(ncGenAccount);
    }

    /**
     * 批量删除应收/付账款
     *
     * @param ids 需要删除的应收/付账款主键
     * @return 结果
     */
    @Override
    public int deleteNcGenAccountByIds(Long[] ids)
    {
        return ncGenAccountMapper.deleteNcGenAccountByIds(ids);
    }

    /**
     * 删除应收/付账款信息
     *
     * @param id 应收/付账款主键
     * @return 结果
     */
    @Override
    public int deleteNcGenAccountById(Long id)
    {
        return ncGenAccountMapper.deleteNcGenAccountById(id);
    }

    @Override
    @Transactional
    public int billAddAccount(Long[] ids) {
        for (int i = 0; i <ids.length ; i++) {
            NcGenAccount acc = ncGenAccountMapper.selectNcGenAccountByWjId(ids[i]);
            if(acc==null){
                NcNoticeBill bill = billMapper.selectNcNoticeBillById(ids[i]);
                acc=new NcGenAccount();
                BeanUtils.copyProperties(bill,acc);
                if("供应商".equals(bill.getPartnerType())){
                    //应付
                    acc.setBillingType("2");
                }
                if("客户".equals(bill.getPartnerType())){
                    //应收
                    acc.setBillingType("1");
                }
                acc.setFactoryName(SecurityUtils.getFactoryName());
                acc.setFactoryCode(SecurityUtils.getFactoryCode());
                acc.setBillingDate(new Date());
                acc.setPartnerEin(bill.getPartnerSh());
                acc.setPartnerAdress(bill.getPartnerDz());
                acc.setPartnerBank(bill.getPartnerKhyh());
                acc.setPartnerAccount(bill.getPartnerZh());
                acc.setPartnerLinkman(bill.getPartnerLxr());
                acc.setPartnerPhone(bill.getPartnerLxdh());
                acc.setContractNo(bill.getContractCode());
                acc.setSignDate(bill.getContractQdr());
                acc.setDeliveryDate(bill.getContractQdrq());
                acc.setSignPerson(bill.getContractQdr());
                acc.setProductNo(bill.getProCode());
                acc.setProductName(bill.getProName());
                acc.setSpecifications(bill.getGgxh());
                acc.setContractAmount(bill.getContractJe());
                acc.setContractPrice(bill.getContractDj());
                acc.setContractNum(bill.getContractNum());
                acc.setKpDate(new Date());
                acc.setPaperType(bill.getInvoiceType());
                acc.setKpNum(bill.getKpSl());
                acc.setKpMoney(bill.getKpJe());
                acc.setKpPrice(bill.getKpDj());
                acc.setKpUser(bill.getChargeUser());
                acc.setId(null);
                ncGenAccountMapper.insertNcGenAccount(acc);
            }else {
                continue;
            }

        }

        return 1;
    }
}
